package com.mdp.workflow.bus.candidate.strategy;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.mdp.workflow.api.FlowProcinstService;
import com.mdp.workflow.bus.candidate.TaskCandidateStrategy;
import com.mdp.workflow.comm.tools.FlowModelUtils;
import com.mdp.workflow.comm.tools.FlowableUtils;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * 9哥
 *
 *@author 唛盟开源9哥
 */
@Component
public class StartUserSelectStrategy implements TaskCandidateStrategy {
 

    @Override
    public com.mdp.workflow.comm.dict.CandidateStrategy getStrategy() {
        return com.mdp.workflow.comm.dict.CandidateStrategy.START_USER_SELECT;
    }

    @Override
    public void validateParam(String param) {

    }

    @Override
    public Set<String> calculateUsers(DelegateExecution execution, String param) {
        return FlowableUtils.getStartUserSelectAssignees(execution);
    }

    @Override
    public boolean isParamRequired() {
        return false;
    }

    /**
     * 获得发起人自选审批人的 UserTask 列表
     *
     * @param bpmnModel BPMN 模型
     * @return UserTask 列表
     */
    public static List<UserTask> getStartUserSelectUserTaskList(BpmnModel bpmnModel) {
        if (bpmnModel == null) {
            return null;
        }
        List<UserTask> userTaskList = FlowModelUtils.getBpmnModelElements(bpmnModel, UserTask.class);
        if (CollUtil.isEmpty(userTaskList)) {
            return null;
        }
        userTaskList.removeIf(userTask -> !Objects.equals(FlowModelUtils.parseCandidateStrategy(userTask),
                com.mdp.workflow.comm.dict.CandidateStrategy.START_USER_SELECT.getStrategy()));
        return userTaskList;
    }

}
